package com.uni.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uni.base.RestResponse;
import com.uni.dao.RolePermsMapper;
import com.uni.pojo.Perm;
import com.uni.pojo.Role;
import com.uni.service.PermService;
import com.uni.service.RoleService;
import com.uni.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者: Unirithe
 * @日期: 2022/2/23
 **/
@RestController
@RequestMapping("/admin/perm")
public class PermAdminController {
    @Autowired
    PermService permService;
    @Autowired
    RoleService roleService;

    @PostMapping("/queryAllWithRole")
    public String queryAllWithRole() {
        // 查询所有角色包括对应的权限
        List<Role> roles = roleService.queryAllROleWithPerm();
        return !roles.isEmpty() ?
                RestResponse.ok(roles).toJSONString()
                : RestResponse.fail("未查询到权限列表").toJSONString();
    }

    @PostMapping("/query/{id}")
    public String queryById(@PathVariable Integer id) {
        Role role = roleService.queryById(id);
        List<Perm> perms = permService.queryPermByRole(new Role() {{
            setId(id);
        }});
        return !perms.isEmpty() ?
                RestResponse.ok(new HashMap<String, Object>() {{
                    put("role", role);
                    put("perms", perms);
                }}).toJSONString()
                : RestResponse.fail("查询失败").toJSONString();
    }

    @PostMapping("/queryAllRolePerm")
    public String queryAll() {
        List<Perm> perms = permService.queryAllPerm();
        return !perms.isEmpty() ? RestResponse.ok(perms).toJSONString()
                : RestResponse.fail("获取所有权限列表失败.").toJSONString();
    }

    @PostMapping("/add")
    public String addPermByRoleIdAndPermId(@RequestBody Map<String, String> map) {
        Integer roleId = Integer.parseInt(map.get("roleId"));
        Integer permId = Integer.parseInt(map.get("permId"));
        int i = roleService.addPerm(roleId, permId);
        return i == 1 ? RestResponse.ok("添加权限成功").toJSONString()
                : RestResponse.fail("添加权限失败.").toJSONString();
    }

    @PostMapping("/delete")
    public String deletePermByRoleIdAndPermId(@RequestBody Map<String, String> map) {
        Integer roleId = Integer.parseInt(map.get("roleId"));
        Integer permId = Integer.parseInt(map.get("permId"));
        int i = roleService.removePermByRole(roleId, permId);
        return i > 0 ? RestResponse.ok("删除权限成功!").toJSONString()
                : RestResponse.fail("删除权限失败.").toJSONString();
    }

    /**
     * 查询所有的权限信息
     * @return
     */
    @PostMapping("/queryAllPerms")
    public String queryAllPerms(@RequestBody(required = false) Map<String, String> map){
        if(map==null) map = new HashMap<>();
        Integer pageNum = Integer.parseInt(map.getOrDefault("pageNum", "1"));
        Integer pageSize = Integer.parseInt(map.getOrDefault("pageSize", "5"));
        PageHelper.startPage(pageNum, pageSize);
        List<Perm> perms = roleService.queryPermsBySome(map);
        PageInfo pageInfo = PageUtil.setPageInfo(pageNum, pageSize, perms);
        return perms == null ?
                RestResponse.fail("查询的权限列表为空").toJSONString()
                : RestResponse.ok(pageInfo).toJSONString();
    }

    /**
     * 根据权限ID查询权限信息
     * @param id 权限的ID
     * @return  JSON格式的结果
     */
    @PostMapping("/queryperm/{id}")
    public String queryPermById(@PathVariable("id") Integer id){
        Perm perm = permService.queryPermById(id);
        return perm == null ?
                RestResponse.fail("未查询到该权限的信息").toJSONString()
                : RestResponse.ok(perm).toJSONString();
    }

    /**
     * 修改权限
     * @param perm
     * @return
     */
    @PostMapping("/modifyperm")
    public String modifyPerm(Perm perm){
        int i = permService.modifyById(perm);
        return i == 0 ? RestResponse.fail("修改权限失败").toJSONString()
                :RestResponse.ok().toJSONString();
    }

    /**
     * 添加权限
     * @param perm 前端表单权限信息
     * @return
     */
    @PostMapping("/addperm")
    public String addPerm(Perm perm){
        int i = permService.addPerm(perm);
        return i == 0 ? RestResponse.fail("添加新权限失败").toJSONString()
                :RestResponse.ok().toJSONString();
    }

    @PostMapping("/permdelete/{id}")
    public String deletePermById(@PathVariable("id") Integer id){
        int i = permService.deletePerm(id);
        return i == 0 ? RestResponse.fail("删除权限失败").toJSONString()
                :RestResponse.ok().toJSONString();
    }
}